package org.example.controller;

import com.alibaba.fastjson.JSONObject;
import de.felixroske.jfxsupport.FXMLController;
import javafx.beans.property.IntegerProperty;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.example.Main;
import org.example.config.FileName;
import org.example.model.ApplicationProcess;
import org.example.model.ApplicationProcessEntity;
import org.example.model.Plan;
import org.example.service.ApplicationProcessService;
import org.example.service.FileHandleService;
import org.example.utils.IdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.*;

/**
 * @Author: wwy
 * @Date: 2020/11/26 13:27
 */
public class AppProcessAddDialogController implements Initializable {
    private static final Logger LOGGER = LoggerFactory.getLogger(AppProcessAddDialogController.class);
    public static ApplicationContext applicationContext;
    public static boolean flag = false;
    @FXML
    private TextField appNameField;
    @FXML
    private TextField appAddressField;

    @FXML
    private TextField pidField;
    @FXML
    private TextField statusNameField;
    @FXML
    private TextField listenPortField;
    @FXML
    private TextField defaultNameField;

    private static Stage dialogStage;

    private static ApplicationProcess currentApplicationProcess;
    private static Plan currentPlan;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        LOGGER.info("AppProcessAdd初始化完成");
        if(currentApplicationProcess != null){
            appNameField.setText(currentApplicationProcess.getAppName());
            appAddressField.setText(currentApplicationProcess.getAppAddress());
            pidField.setText(String.valueOf(currentApplicationProcess.getPid()));
            statusNameField.setText(currentApplicationProcess.getStatusName());
            defaultNameField.setText(currentApplicationProcess.getDefaultName());
            listenPortField.setText(currentApplicationProcess.getListenPort());
        }


    }



    /**
     * 打开新增弹条件出框
     */
    public boolean showApplicationAddDialog(Plan plan) {
        currentApplicationProcess = null;
        currentPlan = plan;
        // Load the fxml file and create a new stage for the popup dialog.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/AppProcessAddDialogView.fxml"));
        try {
            AnchorPane page = (AnchorPane) loader.load();
            // Create the dialog Stage.
            dialogStage = new Stage();
            dialogStage.setTitle("Add appProcess");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            dialogStage.initOwner(Main.getStage());
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);
            dialogStage.showAndWait();
            return flag;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 编辑
     * @param plan
     * @param applicationProcess
     * @return
     */
    public boolean showPlanEditDialog(Plan plan,ApplicationProcess applicationProcess) {
        currentApplicationProcess = applicationProcess;
        currentPlan = plan;
        // Load the fxml file and create a new stage for the popup dialog.
        currentApplicationProcess = applicationProcess;
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/AppProcessAddDialogView.fxml"));
        try {
            AnchorPane page = (AnchorPane) loader.load();
            // Create the dialog Stage.
            dialogStage = new Stage();
            dialogStage.setTitle("edit appProcess");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            dialogStage.initOwner(Main.getStage());
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);
            dialogStage.showAndWait();
            return flag;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 选择文件
     */
    @FXML
    public void fileChoose(){
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open Resource File");
        File file = fileChooser.showOpenDialog(dialogStage);
        System.out.println("文件信息:"+file.getParent()+" "+file.getName()+" "+file.getPath()+" "+file.getAbsolutePath());
        //修改表格中的值
        appNameField.setText(file.getName());
        appAddressField.setText(file.getPath());
    }
    @FXML
    public void handleCancel(){
        dialogStage.close();
        flag = false;
    }
    @FXML
    public void handleOk(){
        ApplicationProcessService applicationProcessService = applicationContext.getBean(ApplicationProcessService.class);
        if(currentApplicationProcess == null){
            //新增
            dialogStage.close();
            flag = true;
            //进行对象封装
            long nextId = IdWorker.getNextId();
            ApplicationProcess applicationProcess = new ApplicationProcess(IdWorker.getNextId(),appNameField.getText(),appAddressField.getText(), pidField.getText(),0,statusNameField.getText(),defaultNameField.getText(),listenPortField.getText());
            System.out.println(applicationProcess.toString());
            ApplicationProcessEntity applicationProcessEntity = new ApplicationProcessEntity();
            applicationProcessEntity.setId(nextId);
            applicationProcessEntity.setAppAddress(appAddressField.getText());
            applicationProcessEntity.setAppName(appNameField.getText());
            applicationProcessEntity.setListenPort(listenPortField.getText());
            applicationProcessEntity.setStatus(0);
            applicationProcessEntity.setStatusName(statusNameField.getText());
            applicationProcessEntity.setDefaultName(defaultNameField.getText());
            applicationProcessEntity.setPlanId(currentPlan.getId());
            applicationProcessService.save(applicationProcessEntity);
            JavaProcessManagementController javaProcessManagementController = applicationContext.getBean(JavaProcessManagementController.class);

            javaProcessManagementController.setApplicationProcess(applicationProcess);

        }else{
            dialogStage.close();
            flag = true;
            //修改
            //获取修改对象的id
            long id = currentPlan.getId();
            //进行对象封装
            ApplicationProcessEntity applicationProcessEntity = new ApplicationProcessEntity();
            applicationProcessEntity.setId(currentApplicationProcess.getId());
            applicationProcessEntity.setAppAddress(appAddressField.getText());
            applicationProcessEntity.setAppName(appNameField.getText());
            applicationProcessEntity.setListenPort(listenPortField.getText());
            applicationProcessEntity.setStatus(0);
            applicationProcessEntity.setStatusName(statusNameField.getText());
            applicationProcessEntity.setDefaultName(defaultNameField.getText());
            applicationProcessEntity.setPlanId(id);
            applicationProcessService.save(applicationProcessEntity);
            JavaProcessManagementController javaProcessManagementController = applicationContext.getBean(JavaProcessManagementController.class);
            ApplicationProcess applicationProcess1 = new ApplicationProcess(
                    currentApplicationProcess.getId()
                    ,appNameField.getText()
                    ,appAddressField.getText()
                    ,pidField.getText()
                    ,0
                    ,statusNameField.getText()
                    ,defaultNameField.getText()
                    ,listenPortField.getText());

            javaProcessManagementController.setEditApplicationProcess(applicationProcess1);


        }

    }
}
